package cn.chenwx.bbsforum.controller;

import cn.chenwx.bbsforum.entity.Comment;
import cn.chenwx.bbsforum.entity.Tag;
import cn.chenwx.bbsforum.entity.User;
import cn.chenwx.bbsforum.mapper.TagMapper;
import cn.chenwx.bbsforum.service.ArticleService;
import cn.chenwx.bbsforum.service.CommentService;
import cn.chenwx.bbsforum.service.TagService;
import cn.chenwx.bbsforum.service.UserService;
import cn.chenwx.bbsforum.utils.BlogJSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.Date;
import java.util.HashMap;

import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private TagService tagService;

    @Autowired
    private   UserService userService;
    @Autowired
    ArticleService articleService;
    @GetMapping(value = {"/index"})
    public String index(Model model, @AuthenticationPrincipal Principal principal, HttpSession session){
        model.addAttribute("userNum",userService.getUserNum());
        User user = userService.findUserByName(principal.getName());
        user.setCreatTime(new Date());

        session.setAttribute("user",user);
        model.addAttribute("username",principal.getName());
        model.addAttribute("userPhone",user.getPhone());
        model.addAttribute("createTime",user.getCreatTimeStr());
        model.addAttribute("articleNum",articleService.getArticle());

        return "admin/admin";
    }

    @GetMapping("/userlist")
    public String userList(Model model){
        return "admin/userList";
    }


    @GetMapping("/getUserList")
    @ResponseBody()
    public Map<String,Object> getUserList(Integer page,int limit){
      //  User  user = (User) session.getAttribute("user");
        PageInfo<User> userPageInfo = new PageInfo<>(userService.findAllUser(page,limit ));
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("data",userPageInfo.getList());
        map.put("count",userPageInfo.getTotal());
        return map;
    }
    @PostMapping("/updateUserStatus")
    @ResponseBody
    public BlogJSONResult updateUserStatus( int status, int id){

       return userService.updateUserStatus(status,id);
    }

    @PostMapping("/updateUser")
    @ResponseBody
    public BlogJSONResult updateUserStatus( User user,  int id){

        userService.updateUser(user,id);
        return BlogJSONResult.ok();
    }

    @GetMapping("/taglist")
    public String getTagList(){
        return "admin/tagList";
    }

    @RequestMapping("/getTags")
    @ResponseBody
    public Map<String,Object> getTags(int page, int limit) {
        PageInfo<Tag> tagPageInfo = new PageInfo<>(tagService.getAllTags(page,limit));
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("data",tagPageInfo.getList());
        map.put("count",tagPageInfo.getTotal());
        return map;
    }
    @Autowired
    private TagMapper tagMapper;

    @PostMapping("/deleteTags")
    @ResponseBody
    public BlogJSONResult deleteTags(int id){
        tagMapper.deleteTags(id);
        return BlogJSONResult.ok();
    }

    @PostMapping("/updateTags")
    @ResponseBody
    public BlogJSONResult updateTags(Tag tag){
        return BlogJSONResult.ok(tagMapper.updateTags(tag));
    }

    @GetMapping("/findTagsCloud")
    @ResponseBody
    public BlogJSONResult getAllTags(){
        return BlogJSONResult.ok(tagService.findTagsCloud());
    }

    @Autowired
    private CommentService commentService;

    @GetMapping("/commentlist")
    public String getComments(){
        return "admin/commentList";
    }
    @RequestMapping("/getCommentList")
    @ResponseBody()
    public Map getCommentList(int page, int limit){
        PageInfo<Comment> tagPageInfo = new PageInfo<>(commentService.getAllComment(page,limit));
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("data",tagPageInfo.getList());
        map.put("count",tagPageInfo.getTotal());
        return map;
    }

    @RequestMapping("/deleteComment")
    @ResponseBody()
    public BlogJSONResult deleteComment(int id){
        commentService.DeleteContentById(id );
        return BlogJSONResult.ok();
    }






}
